package com.haixiaoke.saas.apartment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.apartment.domain.ApartmentLabel;
import com.haixiaoke.saas.apartment.mapper.ApartmentLabelMapper;
import com.haixiaoke.saas.apartment.service.IApartmentLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公寓标签Service业务层处理
 *
 * @author xiaolong
 * @date 2023-10-24
 */
@Service
public class ApartmentLabelServiceImpl implements IApartmentLabelService {
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;

    /**
     * 查询公寓标签
     *
     * @param labelId 公寓标签主键
     * @return 公寓标签
     */
    @Override
    public ApartmentLabel selectApartmentLabelByLabelId(Long labelId) {
        return apartmentLabelMapper.selectApartmentLabelByLabelId(labelId);
    }

    /**
     * 查询公寓标签列表
     *
     * @param apartmentLabel 公寓标签
     * @return 公寓标签
     */
    @Override
    public List<ApartmentLabel> selectApartmentLabelList(ApartmentLabel apartmentLabel) {
        return apartmentLabelMapper.selectApartmentLabelList(apartmentLabel);
    }

    /**
     * 新增公寓标签
     *
     * @param apartmentLabel 公寓标签
     * @return 结果
     */
    @Override
    public ApartmentLabel insertApartmentLabel(ApartmentLabel apartmentLabel) {
        List<ApartmentLabel> apartmentLabels = apartmentLabelMapper.selectApartmentLabelList(apartmentLabel);
        if (CollectionUtil.isNotEmpty(apartmentLabels)) {
            throw new ServiceException("该标签已存在");
        }
        apartmentLabel.setCreateTime(DateUtils.getNowDate());
        int i = apartmentLabelMapper.insertApartmentLabel(apartmentLabel);
        if (i > 0) {
            return apartmentLabel;
        } else {
            throw new ServiceException("系统异常");
        }
    }

    /**
     * 修改公寓标签
     *
     * @param apartmentLabel 公寓标签
     * @return 结果
     */
    @Override
    public int updateApartmentLabel(ApartmentLabel apartmentLabel) {
        apartmentLabel.setUpdateTime(DateUtils.getNowDate());
        return apartmentLabelMapper.updateApartmentLabel(apartmentLabel);
    }

    /**
     * 批量删除公寓标签
     *
     * @param labelIds 需要删除的公寓标签主键
     * @return 结果
     */
    @Override
    public int deleteApartmentLabelByLabelIds(Long[] labelIds) {
        ApartmentLabel apartmentLabel = new ApartmentLabel();
        apartmentLabel.setTenantId(TenantContext.getTenantId());
        apartmentLabel.setLabelIds(labelIds);
        List<ApartmentLabel> apartmentLabelList = apartmentLabelMapper.selectApartmentLabelByLabelIds(apartmentLabel);
        if (apartmentLabelList.size() < labelIds.length) {
            return 0;
        }

        return apartmentLabelMapper.deleteApartmentLabelByLabelIds(labelIds);
    }

    /**
     * 删除公寓标签信息
     *
     * @param labelId 公寓标签主键
     * @return 结果
     */
    @Override
    public int deleteApartmentLabelByLabelId(Long labelId) {
        return apartmentLabelMapper.deleteApartmentLabelByLabelId(labelId);
    }

    @Override
    public List<ApartmentLabel> getAllInfo(String tenantId) {
        return apartmentLabelMapper.getAllInfo(tenantId);
    }

    @Override
    public String selectApartmentLabelNamesByLabelIdIn(List<Long> labelIds) {
        List<ApartmentLabel> labelList = this.selectApartmentLabelByLabelIdIn(labelIds);
        StringBuilder labelNames = new StringBuilder();
        if (CollectionUtil.isNotEmpty(labelList)){
            labelList.forEach(m->labelNames.append(m.getLabelName()).append(','));
        }
        if (StrUtil.isNotBlank(labelNames)) {
            labelNames.deleteCharAt(labelNames.lastIndexOf(","));
        }
        return labelNames.toString();
    }

    public List<ApartmentLabel> selectApartmentLabelByLabelIdIn(List<Long> labelIds) {
        return apartmentLabelMapper.selectApartmentLabelByLabelIdIn(labelIds);
    }
}
